<template>
  <div ref="wrapper_hook" class="echarts_wrapper"/>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'salesmanBarBar',
  props: {
    seriesName: {
      type: String,
      default: '订单'
    },
    source: {
      type: Object,
      default: () => {
        return {
          xAxis: [],
          series: []
        }
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    source(newValue) {
      this._initECharts(newValue)
    }
  },
  mounted() {
    this._initECharts(this.source)
    window.addEventListener('resize', this.resize)
    this.$once('hook:beforeDestroy', () => {
      window.removeEventListener('resize', this.resize)
    })
  },
  destroyed() {
    this.chart.dispose()
  },
  methods: {
    _initECharts(data) {
      this.chart ? this.update(data) : this.draw(data)
    },
    resize(delay = 400) {
      setTimeout(() => {
        this.chart && this.chart.resize()
      }, delay)
    },
    getSeries(data) {
      if (!data || data.length === 0) {
        return 0
      }
      return data.length
    },
    getData(data) {
      let legendData = []
      let certiInspectData = []
      let driverInspectData = []
      let leaveDriverData = []
      data.forEach(item => {
        legendData.push(item.label)
        certiInspectData.push(this.getSeries(item.certiInspect))
        driverInspectData.push(this.getSeries(item.driverInspect))
        leaveDriverData.push(this.getSeries(item.leaveDriver))
      })
      return {
        legendData,
        certiInspectData,
        driverInspectData,
        leaveDriverData
      }
    },
    draw(data) {
      const {xAxis, series} = data
      this.chart = echarts.init(this.$refs.wrapper_hook)
      const options = {
        toolbox: {
          show: true
        },
        tooltip: {
          trigger: 'item',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          top: 20,
          left: 40,
          right: 0,
          bottom: 40
        },
        /*dataZoom: [
          {
            start: 0,
            height: 10,
            bottom: 9,
            end: 60,
            left: 40,
            right: 10
          }],*/
        yAxis: {
          type: 'value',
          axisLabel: {
            fontsize: 14
          },
          scale: true,
          splitNumber: 6,
          axisLine: {
            show: true,
            lineStyle: {
              color: '#575656'
            }
          },
          axisTick: {
            show: false
          }
        },
        xAxis: {
          type: 'category',
          data: xAxis,
          axisLabel: {
            fontsize: 14,
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#575656'
            }
          }
        },

        series: [
          {
            type: 'bar',
            data: series,
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.1)'
            },
            barWidth:40,
            label: {
              normal: {
                show: true,
                formatter: '{c}',
                position: "top",
                textStyle: {
                  fontSize: 13,
                  color: "#787878",
                }
              }
            },
            itemStyle: {
              color: '#28DC32FF'
            }
          }
        ]
      }
      this.chart.setOption(options)
      this.chart.on('click', params => {
        this.$emit('change', params)
      })
    },
    async update(data) {
      if (!this.chart) return
      const {xAxis, series} = data
      const options = this.chart.getOption()
      options.xAxis[0].name = this.seriesName
      options.xAxis[0].data = xAxis
      options.series[0].data = series
      let color = null
      let unit = ''
      if (this.seriesName === '订单') {
        color = {
          color: '#28DC32FF'
        }
        unit = '{c}单'
      } else if (this.seriesName === '欠款') {
        color = {color: '#F9C14EFF'}
        unit = '{c}'
      } else if(this.seriesName === '金额') {
        color = {color: '#28DEF6FF'}
        unit = '{c}'
      }else {
        color = {color: '#188df0'}
        unit = '{c}'
      }
      options.series[0].label.formatter = unit
      options.series[0].itemStyle = color
      await this.$nextTick()
      this.chart.setOption(options, true)
    }
  }
}
</script>

<style scoped>
.echarts_wrapper {
  width: 100%;
  height: 240px;
}
</style>

